package cn.nfwx.tv.down;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;

import com.hdl.m3u8.bean.M3U8;

import java.util.ArrayList;

import cn.nfwx.tv.down.bean.DownloadInfo;
import cn.nfwx.tv.down.bean.RequestInfo;
import cn.nfwx.tv.down.config.InnerConstant;
import cn.nfwx.tv.down.service.DownloadProgramService;
import cn.nfwx.tv.down.utils.LogUtils;

public class DownloadHelper {

    public static final String TAG = "DownloadHelper";

    private volatile static DownloadHelper SINGLETANCE;

    private static ArrayList<RequestInfo> requests = new ArrayList<RequestInfo>();

    private DownloadHelper() {
    }

    public static DownloadHelper getInstance() {
        if (SINGLETANCE == null) {
            synchronized (DownloadHelper.class) {
                if (SINGLETANCE == null) {
                    SINGLETANCE = new DownloadHelper();
                }
            }
        }
        return SINGLETANCE;
    }

    /**
     * 提交  下载/暂停  等任务.(提交就意味着开始执行生效)
     *
     * @param context
     */
    public void submit(Context context) {
        if (requests.isEmpty()) {
            LogUtils.w("没有下载任务可供执行");
            return;
        }
        Intent intent = new Intent(context, DownloadProgramService.class);
        intent.putParcelableArrayListExtra(InnerConstant.Inner.SERVICE_INTENT_EXTRA, requests);
        context.startService(intent);
        requests.clear();
    }// end of "submit(..."


    /**
     * 添加 新的下载任务
     *
     * @param program_name
     * @param time
     * @param u8Url        下载的url
     * @param basePath
     * @param m3u8Path
     * @param m3U8         存储在某个位置上的文件
     * @param action       下载过程会发出广播信息.该参数是广播的action
     * @return DownloadHelper自身 (方便链式调用)
     */
    public DownloadHelper addTask(String program_name, String time, String u8Url, String basePath, String m3u8Path, M3U8 m3U8, @Nullable String action) {
        RequestInfo requestInfo = createRequest(program_name, time, u8Url, basePath, m3u8Path, m3U8, action, InnerConstant.Request.loading);
        LogUtils.i(TAG, "addTask() requestInfo=" + requestInfo);

        requests.add(requestInfo);
        return this;
    }

    /**
     * 暂停某个下载任务
     *
     * @param action 下载过程会发出广播信息.该参数是广播的action
     * @return DownloadHelper自身 (方便链式调用)
     */
    public DownloadHelper pauseTask(String program_name, String time, String u8Url, String basePath, String m3u8Path, @Nullable String action) {
        RequestInfo requestInfo = createRequest(program_name, time, u8Url, basePath, m3u8Path, null, action, InnerConstant.Request.pause);
        LogUtils.i(TAG, "pauseTask() -> requestInfo=" + requestInfo);
        requests.add(requestInfo);
        return this;
    }

    /**
     * @param program_name
     * @param time
     * @param u8Url
     * @param basePath
     * @param m3u8Path
     * @param action
     * @return
     */
    public DownloadHelper refreshTask(String program_name, String time, String u8Url, String basePath, String m3u8Path, @Nullable String action) {
        RequestInfo requestInfo = createRequest(program_name, time, u8Url, basePath, m3u8Path, null, action, InnerConstant.Request.refresh);
        LogUtils.i(TAG, "refreshTask() -> requestInfo=" + requestInfo);
        requests.add(requestInfo);
        return this;
    }

    /**
     * 设定该模块是否输出 debug信息
     * Todo 要重构log模块, 对于我们的静态内部类，目前还不生效
     */
    private DownloadHelper setDebug(boolean isDebug) {
        LogUtils.setDebug(isDebug);
        return this;
    }


    private RequestInfo createRequest(String program_name, String time, String u8Url, String basePath, String m3u8Path, M3U8 m3U8, String action, int dictate) {
        RequestInfo request = new RequestInfo();
        request.setDictate(dictate);
        request.setDownloadInfo(new DownloadInfo(program_name, time, u8Url, basePath, m3u8Path, m3U8, action));
        return request;
    }
}
